/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import ch.ffhs.perellano.volunteers.Stelleninserat;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 *
 * @author perellano
 */
@Stateless
@Path("ch.ffhs.perellano.volunteers.stelleninserat")
public class StelleninseratFacadeREST extends AbstractFacade<Stelleninserat> {

    private EntityManager em;
    private EntityManagerFactory emf;

    public StelleninseratFacadeREST() {
        super(Stelleninserat.class);
        emf = Persistence.createEntityManagerFactory("volunteersDB");
        em = emf.createEntityManager();
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Stelleninserat entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Stelleninserat entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/json"})
    public Stelleninserat find(@PathParam("id") Integer id) {
        return super.find(id);
    }
    
    @GET
    @Path("plz/{plz}")
    @Produces({"application/json"})
    public List<Stelleninserat> findPLZ(@PathParam("plz") String plz) {
        List<Stelleninserat> list = super.findAll();
        List<Stelleninserat> resultList = new ArrayList<Stelleninserat>();
        for (Stelleninserat stelleninserat : list){
            if(stelleninserat.getPlz().equals(plz)){
                resultList.add(stelleninserat);
            }
        }
        return resultList;
    }

    @GET
    @Override
    @Produces({"application/json"})
    public List<Stelleninserat> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/json"})
    public List<Stelleninserat> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public void setEm(EntityManager em) {
        this.em = em;
    }
    
}
